/******************************************************************
 * Created by: GOh CHee Hong                                      
 * Date of Creation: 1/Jan/2009            
 
 *                                                                
 * Description: This class is in charge of polling the peers
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Collections;
namespace Pbdms.BusinessLogic.PeerMS
{
    public class PollingManager
    {
        private bool IsAuthenticating = false;
        private PeerTracker peerTracker;
      
        private Network.NetworkController networkctlr;
        public event EventHandler pollingStarts;
        public event EventHandler pollingFailed;
        public event EventHandler pollingSuccess;
   
        
   
        public PollingManager()
        {

        }

        public PollingManager(PeerTracker tracker,Network.NetworkController networkctlr)
        {
            peerTracker = tracker;
            this.networkctlr = networkctlr;
         
        }
        public void polling()
        {
            //not trusted return;
            if (peerTracker.getTrusted() == false)
            {
                return;
            }

            lock (this)
            {


                if (IsAuthenticating == true)
                {
                    return;
                }
                else
                {
                    IsAuthenticating = true;
                }
            }

            IPAddress peerAddress=peerTracker.getIpAddress();
            string friendKeyID=peerTracker.ID;
            if (pollingStarts != null)
            {
                pollingStarts(peerTracker, EventArgs.Empty);
            }
            if (peerAddress!= null)
            {
                Boolean authenticated=networkctlr.pollingTrustedPeer(peerAddress, friendKeyID);
                if (authenticated == true)
                {
                   
                    if (pollingSuccess != null)
                    {
                        pollingSuccess(peerTracker, EventArgs.Empty);
                    }
                }
                else
                {
                    peerTracker.setIpAddress(null);
                    if (pollingFailed != null)
                    {
                        pollingFailed(peerTracker, EventArgs.Empty);
                    }
                }
            }
            else
            {
                OpenDHTLib.OpenLookUp openDHT = new Pbdms.OpenDHTLib.OpenLookUp();
                string[] listIPs = openDHT.GetStringValues(peerTracker.ID);
                ArrayList IPs = new ArrayList();

                foreach (string ip in listIPs)
                {
                    try
                    {
                        IPAddress realIP =IPAddress.Parse(ip.Trim());
                        IPs.Add(realIP);
                    }
                    catch
                    {

                    }
                }
                //there is no valid IP to poll
                if (IPs.Count == 0)
                {
                    lock (this)
                    {
                        IsAuthenticating = false;
                        return;
                    }
                }
                
                try
                {
                    IPAddress theRealIP = null;
                    foreach (IPAddress unknownIP in IPs)
                    {
                        Boolean authenticated=networkctlr.pollingTrustedPeer(unknownIP, friendKeyID);
                        if (authenticated == true)
                        {
                            theRealIP = unknownIP;
                            break;
                        }
                    }
                    if (theRealIP == null)
                    {
                        if (pollingFailed != null)
                        {
                            pollingFailed(peerTracker, EventArgs.Empty);
                        }
                    }
                    else
                    {
                        peerTracker.setIpAddress(theRealIP);
                        if (pollingSuccess != null)
                        {
                            pollingSuccess(peerTracker, EventArgs.Empty);
                        }
                    }
                }
                catch
                {

                }
            }
            lock (this)
            {
                           
              
                IsAuthenticating = false;
                
            }

        }


        
    
   
    }
}
